/**
 * JogPageBase
 * 
 * A base class for both PageDemo and PageFeatures
 * 
 * @author hovavo
 * 
 */
package leaf.science.pages
{
	import caurina.transitions.Tweener;
	
	import fl.motion.MotionEvent;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.filters.BlurFilter;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	
	import leaf.science.Global;
	import leaf.science.nav.Page;
	import leaf.science.ui.camera.DemosCamera;
	import leaf.science.ui.jogwheel.JogWheel;
	import leaf.science.ui.leftmenu.LeftMenu;
	import leaf.science.ui.rightmenu.RightMenu;

	public class JogPageBase extends Page
	{
		public var leftMenu:LeftMenu;
		public var rightMenu:RightMenu;
		public var jogWheel:JogWheel;
		public var camera:DemosCamera;
		public var camPreloader:CameraPreloader;
		public var jogTip:JogWheelToolTip;
		
		protected var _prevPage:String;
		protected var _nextPage:String;
		protected var _message:String;
		protected var _data:XML;
		protected var _menuItems:Array;
		
		protected var _loader:Loader = new Loader();
		protected var _camSwfURL:String = Global.ROOT_PATH + "swf/DemosCamera.swf";
		
		public function JogPageBase()
		{
			super();
		}
		
		
		// --- IN TRANSITIONS --- //
		
		
		override public function goIn():void
		{
			init();
			visible = true;
			
			_prevPage = LeafScience.instance.prevPageName;
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp)
			if (_prevPage != 'features' && _prevPage != 'demos')
				jogIn();
			else {
				initCamera();
				camera.alpha = 1;
			}
		}
		
		private function jogIn():void
		{
			if (camera) camera.alpha = 0;
			jogWheel.addEventListener(MotionEvent.MOTION_END, onJogInComplete);
			jogWheel.onKnobTweenEnd = onKnobTweenEnd;
			jogWheel.startTransitionIn();
		}
		
		private function onJogInComplete (evt:Event = null):void
		{
			jogWheel.removeEventListener(MotionEvent.MOTION_END, onJogInComplete);
		}
		
		private function onKnobTweenEnd():void
		{
			if (camera && camera.loaded) showJogTip();
			if (!leftMenu.isOpen) leftMenu.open();
			jogWheel.addEventListener( Event.CHANGE, onJogWheelChange );
			if (camera) setTimeout(initCamera, 100)
			else setTimeout(loadCamera, 100);
		}
		
		private function loadCamera ():void
		{
			camPreloader = new CameraPreloader();
			addChildAt(camPreloader, getChildIndex(jogWheel)+1);
			_loader.load( new URLRequest( _camSwfURL ));
		}
		
		private function onLoadInit( evt:Event ):void
		{
			camera = MovieClip( _loader.content ) as DemosCamera;
			_loader = null;
			
			LeafScience.CAMERA = camera;
			camera.alpha = 0
			camera.x = -Math.round( camera.width/2 );
			camera.y = -Math.round( camera.height/2 );
			initCamera();
		}
		
		private function initCamera():void
		{
			jogWheel.range.visible = true;
			addChildAt(camera, getChildIndex(jogWheel)+1);
			camera.addEventListener( Event.CHANGE, onCameraChange );
			
			if (camera.loaded)
			cameraReady();
			else {
				if (!camPreloader || !contains(camPreloader)){
					camPreloader = new CameraPreloader();
					addChildAt(camPreloader, getChildIndex(camera)+1);
					camera.loaderInfo.addEventListener(Event.COMPLETE, onCameraLoadComplete);
				}
				
			}
			
			if (camera.alpha < 1){
				camera.filters = [new BlurFilter(20,20,3)];
				Tweener.addTween(camera, {_blur_blurX:0, _blur_blurY:0 ,alpha:1, time:1, onComplete:onCameraInit});
			} else {
				onCameraInit();
			}
		}	
		
		protected function onCameraInit():void
		{
			// fetaures open right menu here
		}	
		
		protected function onCameraLoadComplete(evt:Event):void
		{
			camera.loaded = true;
			cameraReady();
		}
		
		protected function cameraReady():void
		{
			if (camPreloader) {
				if (contains(camPreloader)) removeChild(camPreloader);
				camPreloader = null;
			}
			jogWheel.enabled = true;
			
			showJogTip();
		}
		
		private function showJogTip():void
		{
			if( !jogTip && _prevPage != 'features' && _prevPage != 'demos' )
			{
				jogTip = new JogWheelToolTip();
				trace('new jogTip')
//				jogTip.y = int(jogWheel.height / -2) + 26;
				jogTip.y = -236;
				addChild( jogTip );
			} else if ( jogTip && !contains(jogTip) && !jogTip.visited){
				trace('showing jogTip')
				jogTip.visible = true;
				addChild( jogTip );
			}
			
			
		}
		
		private function openRightMenu():void
		{
			rightMenu.open();
		}
		
		
		// --- IN TRANSITIONS --- //
		
		
		
		override public function goOut():void
		{
//			if (camera) camera.loaderInfo.removeEventListener(Event.COMPLETE, onCameraLoadComplete);
			if (camPreloader) {
				if (contains(camPreloader)) removeChild(camPreloader);
				camPreloader = null;
			}
			
			removeJogTip();
			closeAllOverlays();
		}
		
		protected function closeAllOverlays():void
		{
			// overriden by each page differently
		}
		
		protected function onOverlayClosed():void
		{
			_nextPage = LeafScience.instance.nextPage.toString();
			if (_nextPage != 'demos' && _nextPage != 'features'){
				leftMenu.close();
				rightMenu.close();
				setTimeout(spinBack, 300);
			} else {
				rightMenu.close();
				setTimeout(onOutComplete, 600);
			}
		}
		
		
		private function spinBack ():void
		{
			if (jogWheel.value < 0.1) jogWheel.value = .99;
			var time:Number = jogWheel.value + 0.2;
			Tweener.addTween(jogWheel, {value:0, time:time});
			setTimeout(fadeOut, time * 200);
		}
		
		private function fadeOut():void
		{
			jogWheel.removeEventListener(MotionEvent.MOTION_END, onJogInComplete);
			jogWheel.addEventListener(MotionEvent.MOTION_END, onFadeOutComplete);
			jogWheel.addChild(camera);
			jogWheel.startTransitionOut();
		}
		
		
		private function onFadeOutComplete (evt:Event):void
		{
			jogWheel.removeEventListener(MotionEvent.MOTION_END, onFadeOutComplete);
			setTimeout(onOutComplete, 50);
		}
		
		
		
		
		
		protected function onRightChange (evt:Event):void
		{
			// overriden by pages
		}
		
		protected function onRightSelect (evt:Event):void
		{
			// overriden by pages
		}
	
		
		private function onLoadProgress( evt:ProgressEvent ):void
		{
			var dest:Number = evt.bytesLoaded/evt.bytesTotal;
			var time:Number = dest-jogWheel.range.end + 0.2;
			Tweener.addTween(jogWheel.range, {end:dest, time:time});
		}
	
		private function onJogWheelChange( evt:Event ):void
		{
			removeJogTip();
			if (camera is DemosCamera) DemosCamera( camera ).closeAllToolTipsNow();
			if( camera )
				camera.frame = Math.round( camera.totalFrames * jogWheel.value );
		}
		
		protected function removeJogTip():void
		{
			if( jogTip && !jogTip.visited )
			{
				jogTip.visible = false;
				removeChild( jogTip );
				jogTip.visited = true;
			}
		}
		
		private function onCameraChange( evt:Event ):void
		{
			jogWheel.value = camera.angle/360;
		}
		
		protected function onCameraClick (evt:MouseEvent):void
		{
			// overriden by pages
		}
		
		protected function onKeyUp(evt:KeyboardEvent):void
		{
			// overriden by pages
		}
		
		protected function init ():void
		{
			if (!LeafScience.LEFT_MENU){
				leftMenu = new LeftMenu();
				LeafScience.LEFT_MENU = leftMenu;
			} else {
				leftMenu = LeafScience.LEFT_MENU;
			}
			leftMenu.selectedName = this.toString();
			addChild(leftMenu);
			
			if (rightMenu && contains(rightMenu)){
				removeChild(rightMenu);
				rightMenu = null;
			}
			
			rightMenu = new RightMenu(_message);
			addChild(rightMenu);
			
			rightMenu.addEventListener(Event.CHANGE, onRightChange);
			rightMenu.addEventListener(Event.SELECT, onRightSelect);
			rightMenu.data = _menuItems;
			
			if (!LeafScience.JOGWHEEL){
				jogWheel = new JogWheel();
				jogWheel.alpha = 0;
				LeafScience.JOGWHEEL = jogWheel;
			} else {
				jogWheel = LeafScience.JOGWHEEL;
			}
			addChild( jogWheel );
			jogWheel.enabled = false;
			
			if (!LeafScience.CAMERA){
				_loader = new Loader;
				_loader.mouseEnabled = false;
				_loader.contentLoaderInfo.addEventListener( Event.INIT, onLoadInit );
				_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onCameraLoadComplete );
				_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onLoadProgress );
			} else {
				camera = LeafScience.CAMERA;
				if (camera.hotspotsVisible) camera.hideHotspots();
			}
			
			visible = false;
		}
	}
}